import 'package:cached_network_image/cached_network_image.dart';
import 'package:clean_app/asset/color.dart';
import 'package:clean_app/asset/textstyle.dart';
import 'package:clean_app/pages/vision/vision.dart';
import 'package:clean_app/provider/task_provider.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';

class UploadedTasks extends StatefulWidget {
  UploadedTasks({super.key});

  @override
  State<UploadedTasks> createState() => _UploadedTasksState();
}

class _UploadedTasksState extends State<UploadedTasks> {
  getData() async {
    var _p = Provider.of<TaskProvider>(context, listen: false);
    _p.getData();
  }

  @override
  void initState() {
    getData();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    var _p = Provider.of<TaskProvider>(context);
    return Scaffold(
      backgroundColor: os_back,
      appBar: AppBar(
        backgroundColor: os_back,
        leading: IconButton(
          onPressed: () {
            Navigator.of(context).pop();
          },
          icon: Icon(Icons.chevron_left),
        ),
      ),
      body: ListView(
        children: [
          ..._p.uploadedData.map((ele) {
            return Card(
              elevation: 0,
              color: os_white,
              margin: EdgeInsets.only(left: 12.5, right: 12.5, bottom: 12.5),
              child: Padding(
                padding: EdgeInsets.all(20.0),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Row(
                      children: [
                        Icon(
                          Icons.location_on_outlined,
                          color: os_color,
                        ),
                        Text(
                          " 拍摄位置",
                          style: XSTextStyle(
                            context: context,
                            color: os_black,
                            fontSize: 18,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                      ],
                    ),
                    Container(height: 7.5),
                    Text(
                      ele["description"],
                      style: XSTextStyle(
                        context: context,
                        color: os_light_light_dark_card,
                        fontSize: 16,
                        fontWeight: FontWeight.normal,
                      ),
                    ),
                    Container(height: 7.5),
                    Container(
                      height: 1,
                      color: os_grey,
                      margin: EdgeInsets.only(top: 10, bottom: 10),
                    ),
                    Container(height: 10),
                    ConstrainedBox(
                      constraints: BoxConstraints(
                        maxWidth: 200,
                      ),
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(10),
                        child: CachedNetworkImage(
                          imageUrl: ele["thumbnail_url"],
                          width: 200,
                          height: 250,
                          fit: BoxFit.cover,
                          placeholder: (context, url) {
                            return Container(
                              color: os_grey,
                              width: 200,
                              height: 200,
                            );
                          },
                        ),
                      ),
                    ),
                    Container(height: 10),
                    Container(
                      height: 1,
                      color: os_grey,
                      margin: EdgeInsets.only(top: 10, bottom: 10),
                    ),
                    Container(height: 5),
                    Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Row(
                          children: [
                            Icon(
                              Icons.remove_red_eye_outlined,
                              color: os_deep_grey,
                              size: 18,
                            ),
                            Container(width: 5),
                            Text(
                              "${ele["submission_time"].toString().substring(0, 10)} ${ele["submission_time"].toString().substring(11, 16)}",
                              style: XSTextStyle(
                                context: context,
                                color: os_deep_grey,
                                fontSize: 15,
                                fontWeight: FontWeight.normal,
                              ),
                            ),
                          ],
                        ),
                        TextButton(
                          onPressed: () {
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) => VisionTab(
                                  imageUrl: ele["thumbnail_url"],
                                ),
                              ),
                            );
                          },
                          style: ButtonStyle(
                            backgroundColor:
                                WidgetStateProperty.all(os_wonderful_color[3]),
                            foregroundColor: WidgetStateProperty.all(os_white),
                          ),
                          child: Text(
                            "  问问AI  ",
                            style: XSTextStyle(
                              context: context,
                              color: os_white,
                              fontSize: 15,
                              fontWeight: FontWeight.bold,
                            ),
                          ),
                        ),
                        // TextButton(
                        //   onPressed: () {
                        //     Navigator.of(context)
                        //         .pushNamed(
                        //       "/process_img_edit",
                        //       arguments: ele,
                        //     )
                        //         .then((_) {
                        //       getData();
                        //     });
                        //   },
                        //   style: ButtonStyle(
                        //     backgroundColor: WidgetStateProperty.all(os_color),
                        //     foregroundColor: WidgetStateProperty.all(os_white),
                        //   ),
                        //   child: Text(
                        //     "编辑",
                        //     style: XSTextStyle(
                        //       context: context,
                        //       color: os_white,
                        //       fontSize: 15,
                        //       fontWeight: FontWeight.bold,
                        //     ),
                        //   ),
                        // ),
                      ],
                    ),
                  ],
                ),
              ),
            );
          }).toList(),
        ],
      ),
    );
  }
}
